import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baidu.translate.demo.TransApi;
import jdk.nashorn.internal.parser.JSONParser;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

public class Main {

    // 在平台申请的APP_ID 详见 http://api.fanyi.baidu.com/api/trans/product/desktop?req=developer
    private static String APP_ID;
    private static String SECURITY_KEY;
    static TransApi api;
    static int ms;

    public static void main(String[] args) throws InterruptedException, IOException {
        System.out.println("PRINTMODE OR FILEMODE?PRINTMODE=TRUE,FILEMODE=FALSE");
        boolean printMode=parseBool(new Scanner(System.in).next());
        System.out.println("CURRENTLY USING BAIDU TRANSLATE");
        System.out.println("PLEASE INPUT APPID:");
        APP_ID = new Scanner(System.in).next();
        System.out.println("PLEASE INPUT SECUIRTY_KEY:");
        SECURITY_KEY = new Scanner(System.in).next();
        api=new TransApi(APP_ID,SECURITY_KEY);
       if (printMode){
            System.out.println("PLEASE INPUT SOURCE:");
            String source = new Scanner(System.in).next();
            System.out.println("PLEASE INPUT TIMES:");
            int ts = new Scanner(System.in).nextInt();
            System.out.println("PLEASE INPUT DELAY(MILLI SECONDS):");
            ms = new Scanner(System.in).nextInt();
            //System.out.println("OUTPUT TO FILE?(PATH:./result.txt)");
            //boolean outPut=parseBool(new Scanner(System.in).next());
            System.out.println(shengCao(source, ts));
        }else {
           String source = readFileByLines("./in.txt");
           System.out.println("PLEASE INPUT TIMES:");
           int ts = new Scanner(System.in).nextInt();
           FileOutputStream fileOutputStream=null;
           String result=shengCao(source, ts);
           try{
               fileOutputStream=new FileOutputStream("./out.txt");
           }catch (Exception ignored){
               //
           }
           if (fileOutputStream==null){
               new File("./out.txt").createNewFile();
               fileOutputStream=new FileOutputStream("./out.txt");
           }
           fileOutputStream.write(result.getBytes());
           fileOutputStream.close();
       }
    }
    public static String readFileByLines(String filename){
        StringBuilder sb=new StringBuilder();
        File file=new File(filename);
        BufferedReader reader=null;
        try {
            reader=new BufferedReader(new FileReader(file));
            String str=null;
            int line=1;
            while ((str=reader.readLine())!=null){
                sb.append(str);
                line++;
            }
            reader.close();
        } catch (IOException e) {

        }finally {
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
        return sb.toString();
    }

    static String translate(String source, String from, String to){
        JSONObject jsonObject=JSONObject.parseObject((api.getTransResult(source, from, to)));
        JSONArray res= (JSONArray) jsonObject.get("trans_result");
        JSONObject res1=JSONObject.parseObject(res.getString(0));
        //return jsonObject1.toString();
        //return res.get("dst");
        return res1.getString("dst");
    }
    static TranslateResult translateToResult(String source, String from, String to){
        long start=System.currentTimeMillis();
        JSONObject jsonObject=JSONObject.parseObject((api.getTransResult(source, from, to)));
        JSONArray res= (JSONArray) jsonObject.get("trans_result");
        JSONObject res1=JSONObject.parseObject(res.getString(0));
        //return jsonObject1.toString();
        //return res.get("dst");
        return new TranslateResult(res1.getString("dst"),System.currentTimeMillis()-start);
    }

    static String shengCao(String zhSource,int times) throws InterruptedException {
        String str=zhSource;
        String lang="zh";
        for (int i=0;i<times*2;i++){
            if (i==times*2-1){
                str=translate(str,lang,"zh");
            }else {
                String from=lang;
                String to=randomLang();
                while (from.equals(to)){
                    to=randomLang();
                    //CHOOSE AGAIN
                }
                str = translate(str, from,to);
                System.out.println("from:"+from + " to:"+to);
                lang=to;
            }
            System.out.println((i+1) +"/"+times*2);
            if (i!=times*2-1){
                Thread.sleep(ms);
            }
        }
        return str;
    }
    static class TranslateResult{
        String result;
        long ms;
        public TranslateResult(String result,long ms){
            this.result=result;
            this.ms=ms;
        }
    }
    enum Languages{
        yue,
        kor,
        th,
        pt,
        el,
        bul,
        fin,
        slo,
        cht,
        zh,
        wyw,
        fra,
        ara,
        de,
        nl,
        est,
        cs,
        swe,
        vie,
        en,
        jp,
        spa,
        ru,
        it,
        pl,
        dan,
        rom,
        hu
    }
    static Random random=new Random();
   static String randomLang(){
        int num=random.nextInt(Languages.values().length-1);
        return Languages.values()[num].toString();
    }
   static boolean parseBool(String s){
       switch (s.toLowerCase())
       {
           case "yes":
           case "true":
           case "t":
           case "y":
           case "ye":
           case  "ok":
               return true;

           case "no":
           case "false":
           case "f":
           case "n":
           case "not":
               return false;

           default:
               throw new RuntimeException("I don't understand your boolean input!");



       }
    }

}
